package com.jiang.redis.single;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.StopWatch;
import cn.hutool.core.lang.Console;
import cn.hutool.core.thread.ThreadUtil;
import com.jiang.redis.redisson.RedissonUtils;
import lombok.Data;
import org.junit.Test;
import org.redisson.api.RLock;

import java.util.concurrent.TimeUnit;

/**
 * @author jiangwz
 * @create 2021/7/26.
 */
public class TestRedisson {


    @Test
    public void test01(){
        String lockKey = "test-" + 100;
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        RLock lock = RedissonUtils.lock(lockKey,  TimeUnit.SECONDS, 10);
        ThreadUtil.sleep(20L, TimeUnit.MINUTES);
        RedissonUtils.unlock(lock);
        stopWatch.stop();
        System.out.println(stopWatch.getTotalTimeMillis());
    }

    @Test
    public void test02(){
        String lockKey = "test-" + 100;
        boolean tryLock = RedissonUtils.tryLock(lockKey, TimeUnit.SECONDS, 20, 30);
        System.out.println(tryLock);
        ThreadUtil.sleep(20L, TimeUnit.MINUTES);
        RedissonUtils.lock(lockKey);
//        System.out.println("lock");
    }

    @Test
    public void test03(){
        for (int i = 0; i < 10; i++) {
            String lockKey = "test-" + 100;
            boolean tryLock = RedissonUtils.tryLock(lockKey, TimeUnit.SECONDS, 1, 5);
            Console.log("date=[{}],lock=[{}]", DateUtil.now(), tryLock);
        }
    }

    @Test
    public void test04(){
        String lockKey = "test-" + 100;
        boolean tryLock = RedissonUtils.tryLock(lockKey, TimeUnit.SECONDS, 20, 20);
        if (tryLock) {
            RLock lock = RedissonUtils.getLock(lockKey);
            RedissonUtils.unlock(lock);
        }

    }

}
@Data
class Person {
    private Long id;
    private String name;
}
